import sys
from socket import *
port = 50008
host = 'localhost'

def initListenerSocket(port = port):
    sock = socket(AF_INET, SOCK_STREAM)
#	sock.setblocking(0)
#	sock.settimeout(2)
	sock.bind(('', port))
	sock.listen(5)
	conn, addr = sock.accept()
	return conn

def redirectOut(port = port, host = host):
	sock = socket(AF_INET, SOCK_STREAM)
#	sock.setblocking(0)
#	sock.settimeout(2)
	sock.connect((host, port))
	file = sock.makefile('w')
	sys.stdout = file
	return sock

def redirectIn(port = port, host = host):
	sock = socket(AF_INET, SOCK_STREAM)
#	sock.setblocking(0)
#	sock.settimeout(2)
	sock.connect((host, port))
	file = sock.makefile('r')
	sys.stdin = file
	return sock

def redirectBothAsClient(port = port, host = host):
	sock = socket(AF_INET, SOCK_STREAM)
#	sock.setblocking(0)
#	sock.settimeout(2)
	sock.connect((host, port))
	ofile = sock.makefile('w')
	ifile = sock.makefile('r')
	sys.stdout = ofile
	sys.stdin = ifile
	return sock

def redirectBothAsServer(port = port, host = host):
	sock = socket(AF_INET, SOCK_STREAM)
#	sock.settimeout(2)
	sock.bind((host, port))
	sock.listen(5)
	conn, addr = sock.accept()	
	ofile = sock.makefile('w')
	ifile = sock.makefile('r')
	sys.stdout = ofile
	sys.stdin = ifile
	return conn